package com.shire.shire_interface.controller.timeline;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.timeline.WxTimelineTask;
import com.shire.shire_interface.model.timeline.WxTimelineTaskExample;
import com.shire.shire_interface.service.timeline.WxTimelineTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Api(tags = "发布朋友圈接口")
@RestController
@RequestMapping("/timeline")
public class WxTimelineTaskController extends BaseController {
    @Resource
    private WxTimelineTaskService timelineTaskService;

//    @ApiOperation(value = "查询朋友圈发送记录，post body里以json格试传 csId, pageSize, pageNo")
    @ResponseBody
    @PostMapping("/getTimelines")
    public Response getTimelineTasks(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            Integer pageSize = jsonObject.getInteger("pageSize");
            Integer pageNo = jsonObject.getInteger("pageNo");

            WxTimelineTaskExample example = new WxTimelineTaskExample();
            example.createCriteria().andCsidEqualTo(csId);
            example.setOrderByClause("id desc");
            example.setLimitStart(getPageStartRow(pageNo, pageSize));
            example.setLimitSize(pageSize);

            List<WxTimelineTask> timelineTaskList = timelineTaskService.getWxTimelineTask(example);
            response.success("成功");
            response.setData(timelineTaskList);
        }catch (Exception e){
            response.error(e.getMessage());
            e.printStackTrace();
        }

        return response;
    }

//    @ApiOperation(value = "插入朋友圈任务， wxAccount:发送帐号， csId:客服ID, timelineContent:发送内容，sendType:0即时，1定时，sendType,sendType:定时发送时间")
    @ResponseBody
    @PostMapping("/createTimeline")
    public Response createTimeline(@RequestBody JSONObject wxTimelineTask, HttpSession session){
        Response response = new Response();

        try {
            List<WxTimelineTask> dataList = new ArrayList<>();
            for (Object wxAccount : wxTimelineTask.getJSONArray("wxAccount")) {
                Integer csId = wxTimelineTask.getInteger("csId");
                if (!checkLoginUserByCsId(session, csId)){
                    response.error("请登录！");
                    response.setCode(501);
                    return response;
                }
                WxTimelineTask task = new WxTimelineTask();
                task.setWxaccount((String) wxAccount);
                task.setCsid(csId);
                task.setTimelinecontent(wxTimelineTask.getString("timelineContent"));
                task.setSendtype(wxTimelineTask.getInteger("sendType"));
                if (wxTimelineTask.containsKey("sendtime"))
                    task.setSendtime(wxTimelineTask.getDate("sendtime"));
                else
                    task.setSendtime(new Date());
                task.setCreatetime(new Date());
                task.setStatus(0);
                timelineTaskService.inserTimelineTask(task);
                dataList.add(task);
            }
            response.setData(dataList);
            response.success("成功");
        }catch (Exception e){
            response.error(e.getMessage());
            e.printStackTrace();
        }

        return response;
    }

//    @ApiOperation(value = "取消朋友圈任务，id:任务id")
    @ResponseBody
    @PostMapping("/cancelTimeline")
    public Response cancelTimelineTask(@RequestBody WxTimelineTask wxTimelineTask, HttpSession session){
        Response response = new Response();

        try {

            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            if (wxTimelineTask.getId() == null){
                response.error("参数错误");
                return response;
            }

            WxTimelineTask timelineTask = timelineTaskService.getTimelineById(wxTimelineTask.getId());
            timelineTask.setStatus(2);
            timelineTaskService.updateWxTimelineTask(timelineTask);

            response.success("成功");
        }catch (Exception e){
            response.error(e.getMessage());
            e.printStackTrace();
        }

        return response;
    }
}
